package com.hxkj.service.impl;

import com.hxkj.client.service.SiteServiceClient;
import com.hxkj.client.service.UserSiteCollectServiceClient;
import com.hxkj.client.service.WebsiteAddrServiceClient;
import com.hxkj.domain.po.Site;
import com.hxkj.domain.po.UserSiteCollect;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.entity.vo.WebsiteAddrDetail;
import com.hxkj.service.UserSiteCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * created by kezhijie on 2018/9/10 14:08.
 * email to kezhijie@hxoss.com
 */
@Service
public class UserSiteCollectServiceImpl implements UserSiteCollectService {

    @Autowired
    private UserSiteCollectServiceClient userSiteCollectServiceClient;
    @Autowired
    private SiteServiceClient siteServiceClient;
    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;

    @Override
    public List<WebsiteAddrDetail> collectList(String userId) throws Exception {
        UserSiteCollect userSiteCollect = new UserSiteCollect();
        userSiteCollect.setUserId(userId);

        List<UserSiteCollect> userSiteCollects = userSiteCollectServiceClient.get(userSiteCollect);

        List<WebsiteAddrDetail> websiteAddrDetails = new ArrayList<>();

        for (UserSiteCollect userSiteCollect1 : userSiteCollects) {

            Site siteCondition = new Site();
            siteCondition.setSiteId(userSiteCollect1.getSiteId());

            Site site = siteServiceClient.getSite(siteCondition).getData();

            WebsiteAddr websiteCondition = new WebsiteAddr();
            websiteCondition.setSiteId(userSiteCollect1.getSiteId());

            List<WebsiteAddr> websiteAddrs = websiteAddrServiceClient.getList(websiteCondition);

            for (WebsiteAddr websiteAddr : websiteAddrs) {
                WebsiteAddrDetail websiteAddrDetail = buildWebsiteAddrDetail(websiteAddr);
                websiteAddrDetail.setSiteName(site.getSiteName());
                websiteAddrDetails.add(websiteAddrDetail);
            }

        }
        return websiteAddrDetails;
    }

    private WebsiteAddrDetail buildWebsiteAddrDetail(WebsiteAddr websiteAddr) {
        WebsiteAddrDetail websiteAddrDetail = new WebsiteAddrDetail();
        if (Objects.nonNull(websiteAddr.getAddrId()))
            websiteAddrDetail.setAddrId(websiteAddr.getAddrId());
        if (StringUtils.hasText(websiteAddr.getAddrIds()))
            websiteAddrDetail.setAddrIds(websiteAddr.getAddrIds());
        if (Objects.nonNull(websiteAddr.getCreateor()))
            websiteAddrDetail.setCreateor(websiteAddr.getCreateor());
        if (Objects.nonNull(websiteAddr.getCreateTime()))
            websiteAddrDetail.setCreateTime(websiteAddr.getCreateTime());
        if (Objects.nonNull(websiteAddr.getDelTag()))
            websiteAddrDetail.setDelTag(websiteAddr.getDelTag());
        if (StringUtils.hasText(websiteAddr.getIpAdd())){
            if (!websiteAddr.getIpAdd().startsWith("http://"))
                websiteAddrDetail.setIpAdd("http://" + websiteAddr.getIpAdd());
            else
                websiteAddrDetail.setIpAdd(websiteAddr.getIpAdd());
        }
        if (StringUtils.hasText(websiteAddr.getIpPort()))
            websiteAddrDetail.setIpPort(websiteAddr.getIpPort());
        if (Objects.nonNull(websiteAddr.getModifyBy()))
            websiteAddrDetail.setModifyBy(websiteAddr.getModifyBy());
        if (Objects.nonNull(websiteAddr.getSiteId()))
            websiteAddrDetail.setSiteId(websiteAddr.getSiteId());
        if (StringUtils.hasText(websiteAddr.getSourcesDirectory()))
            websiteAddrDetail.setSourcesDirectory(websiteAddr.getSourcesDirectory());
        if (StringUtils.hasText(websiteAddr.getTargetName()))
            websiteAddrDetail.setTargetName(websiteAddr.getTargetName());
        if (Objects.nonNull(websiteAddr.getUpdateTime()))
            websiteAddrDetail.setUpdateTime(websiteAddr.getUpdateTime());
        if (StringUtils.hasText(websiteAddr.getUrlPrefix()))
            websiteAddrDetail.setUrlPrefix(websiteAddr.getUrlPrefix());
        if (StringUtils.hasText(websiteAddr.getUrlRegular()))
            websiteAddrDetail.setUrlRegular(websiteAddr.getUrlRegular());
        return websiteAddrDetail;
    }
}
